package com.thoughtworks.biblioteca;

import java.sql.Connection;
import java.sql.PreparedStatement;

public class UserDB {
	Connection connectionToBibliotecaDatabase = null;
	
	public UserDB(){
		connectionToBibliotecaDatabase=MySQLConnection.obtainConnection();
	}

	public void addUser(String userName) {
		String password = new String("ns");
		try{
		PreparedStatement preparedInsertUserStatement;
		String insertNewUser = "insert into users values (?,?)";
		preparedInsertUserStatement = connectionToBibliotecaDatabase.prepareStatement(insertNewUser);
		preparedInsertUserStatement.setString(1, userName);
		preparedInsertUserStatement.setString(2, password);
		preparedInsertUserStatement.executeUpdate();
		}
		catch(Exception e){
		}
	}
	public void removeUser(String userName){
		try{
			PreparedStatement preparedDeleteUserStatement;
			String deleteUser = "delete from users where username=?";
			preparedDeleteUserStatement = connectionToBibliotecaDatabase.prepareStatement(deleteUser);
			preparedDeleteUserStatement.setString(1, userName);
			preparedDeleteUserStatement.executeUpdate();
			}
			catch(Exception e){
			}
	}

	public void setPassword(String userName,String newPassword) {
		try {
			PreparedStatement preparedInsertPasswordStatement;
			String setUserPassword = "update users set password = ? where username=?;";
			preparedInsertPasswordStatement = connectionToBibliotecaDatabase.prepareStatement(setUserPassword);
			preparedInsertPasswordStatement.setString(1, newPassword);
			preparedInsertPasswordStatement.setString(2, userName);
			preparedInsertPasswordStatement.executeUpdate();
		} catch (Exception e) {
		}
	}

}
